package com.yty.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yty.dao.ClazzMapper;
import com.yty.dao.StudentMapper;
import com.yty.page.PageResult;
import com.yty.param.ClazzQueryParam;
import com.yty.pojo.Clazz;
import com.yty.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper classzzMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult findAll(ClazzQueryParam clazz) {
        PageHelper.startPage(clazz.getPage(),clazz.getPageSize());
        Page<Clazz> classlist = (Page<Clazz>) classzzMapper.findAll(clazz);
        return new PageResult(classlist.getTotal(),classlist.getResult());
    }

    @Override
    public int addClazz(Clazz clazz) {
        if (clazz==null || clazz.getName()==null || clazz.getName().equals("")
            || clazz.getRoom()==null || clazz.getRoom().equals("")
            || clazz.getBeginDate()==null || clazz.getEndDate()==null || clazz.getSubject()==null){
            return 0;
        }
        int i = classzzMapper.addClazz(clazz);
        return clazz.getId();
    }

    @Override
    public int deleteClazzById(Integer... ids) {
        Integer n = studentMapper.findClazzIsExist(ids);
        if (n!=0)return 0;
        int count = classzzMapper.deleteClazzById(ids);
        return count;
    }

    @Override
    public Clazz findClazzById(Integer id) {
        Clazz clazz = classzzMapper.findClazzById(id);
        return clazz;
    }

    @Override
    public int updateClazz(Clazz clazz) {
        if (clazz==null || clazz.getName()==null || clazz.getName().equals("")
                || clazz.getRoom()==null || clazz.getRoom().equals("")
                || clazz.getBeginDate()==null || clazz.getEndDate()==null || clazz.getSubject()==null){
            return 0;
        }
        int i = classzzMapper.updateClazz(clazz);
        return i;
    }

    @Override
    public List<Clazz> findAllList() {
        ClazzQueryParam param = new ClazzQueryParam();
        List<Clazz> clazzes = classzzMapper.findAll(param);
        return clazzes;
    }
}
